package cn.kpi.service.loan.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.loan.LoanUserDao;
import cn.kpi.entity.loan.LoanUserEntity;
import cn.kpi.entity.loan.LoanUserListEntity;
import cn.kpi.entity.loan.LoanUserLogEntity;
import cn.kpi.service.loan.ILoanUserService;

@Service
public class LoanUserServiceImpl implements ILoanUserService{
	@Autowired
	private LoanUserDao loanUserDao;


	public int LoanTask()throws Exception{
		try{
			int result=0;
			List<LoanUserListEntity> list=loanUserDao.GetLoanListForTask();
			if(list.size()>0){
				result+=loanUserDao.UpdateLoanListForTask(list);
			}

			return  result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}

	@Transactional
	public int InsertLoanUser(LoanUserEntity entity)throws Exception{
		try{
			int result=0;
			result+=loanUserDao.InsertLoanUserEntity(entity);
			LoanUserLogEntity entitylog=new LoanUserLogEntity();
			entitylog.setLoanlogdes(entity.getLoandes());
			entitylog.setStatus(entity.getStatus());
			entitylog.setLoanid(entity.getLoanid());
			entitylog.setByuserid(entity.getByuserid());
			result+=loanUserDao.InsertLoanUserLogEntity(entitylog);
			if(result!=2){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return -1;
			}
			return result;
		}
		catch(Exception ex){
			return -1;
		}
	}
	public List<LoanUserListEntity> GetUserLoanList(String where,String realname)throws Exception{
		try{
			return loanUserDao.GetUserLoanList(where,realname);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	public int GetUserLoanListCount(String where,String realname)throws Exception{
		try{
			return loanUserDao.GetUserLoanListCount(where,realname);
		}
		catch(Exception ex){
			return -1;
		}
	}
	@Transactional
	public int UpdateLoanUser(Long loanid,Long loanorgid,String loandes,int status,Long userid,Long gdbyuserid)throws Exception{
		try{
			int result=0;
			result+=loanUserDao.UpdateLoanUser(loandes, loanid, userid, status,gdbyuserid);
			LoanUserLogEntity entitylog=new LoanUserLogEntity();
			entitylog.setLoanlogdes(loandes);
			entitylog.setStatus(status);
			entitylog.setLoanid(loanid);
			entitylog.setByuserid(gdbyuserid);
			result+=loanUserDao.InsertLoanUserLogEntity(entitylog);
//			if(result!=2){
//				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//				return -1;
//			}
			if(status  == 2) {//归档修改用户组织机构
//				result += loanUserDao.UpdateUserSzdwByUserid(loanorgid, userid);
				//归档修改用户组织机构
				LoanUserListEntity loadentity=loanUserDao.GetUserLoanByLoanid(loanid);
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				if(loadentity!=null){
					Date begintime=loadentity.getBegintime();
					Calendar aCalendar = Calendar.getInstance();
					aCalendar.setTime(new Date());
					int dayNow = aCalendar.get(Calendar.DAY_OF_YEAR);
					aCalendar.setTime(begintime);
					int dayBegin = aCalendar.get(Calendar.DAY_OF_YEAR);
					int days=dayNow-dayBegin;
					if(days>=0){
						result += loanUserDao.UpdateUserSzdwByUserid(loadentity.getLoanorgid(), userid);
					}
				}
			}
		
			return result;
		}
		catch(Exception ex){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	
	@Transactional
	public int UpdateLoanUserJs(Long loanid,String loandes,int status,Long userid,Long gdbyuserid,Long loanorgid)throws Exception{
		try{
			int result=0;
			result+=loanUserDao.UpdateLoanUser(loandes, loanid, userid, status,gdbyuserid);
			LoanUserLogEntity entitylog=new LoanUserLogEntity();
			entitylog.setLoanlogdes(loandes);
			entitylog.setStatus(status);
			entitylog.setLoanid(loanid);
			entitylog.setByuserid(gdbyuserid);
			result+=loanUserDao.InsertLoanUserLogEntity(entitylog);
//			if(result!=2){
//				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//				return -1;
//			}
			
//			result+=loanUserDao.UpdateUserSzdwByUserid(loanorgid,userid);
			return result;
		}
		catch(Exception ex){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	
	public LoanUserListEntity GetUserLoanByLoanid(Long loanid)throws Exception{
		try{
			return loanUserDao.GetUserLoanByLoanid(loanid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public int UpdateLoanUserEntity(LoanUserEntity entity)throws Exception{
		try{
			return loanUserDao.UpdateLoanUserEntity(entity);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	
	@Transactional
	public int DeleteUserLoan(Long loanid)throws Exception{
		try{
			int result=0;
			result+=loanUserDao.DeleteUserLoanLog(loanid);
			result+=loanUserDao.DeleteUserLoan(loanid);
			return result;
		}
		catch(Exception ex){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	
	public List<LoanUserListEntity> GetUserLoanListDq(String where)throws Exception{
		try{
			return loanUserDao.GetUserLoanListDq(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}


}
